import config from "../config.js";
const openIdUrl = config;
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//正则判断
function Regular(str, reg) {
  if (reg.test(str))
    return true;
  return false;
}

//是否为中文
function IsChinese(str) {
  var reg = /^[\u0391-\uFFE5]+$/;
  return Regular(str, reg);
}

//检查是否关闭地理位置信息获取授权
function checkLocationIsOff(obj) {
  var flag = null;
  wx.getLocation({
    type: 'gcj02',
    success: function(res) {
      var latitude = res.latitude
      var longitude = res.longitude
      obj.setData({
        showLocationTip: false
      })
    },
    fail: function(res) {
      console.log('拒绝授权')
      obj.setData({
        showLocationTip: true
      })
    }
  })


  return flag;
}

function getAgreement(id) {
  wx.request({
    url: openIdUrl.agreemenDetail,
    data: {
      id: id,
    },
    success: function(res) {
      console.log(res);
      wx.hideLoading();
      if (res.data.status == 200) {
        var url = res.data.data.url;
        wx.setStorageSync("webviewUrl", url);
        wx.navigateTo({
          url: "../webview/webview"
        })

      } else {
        wx.hideLoading();
        wx.showToast({
          title: res.data.message + "",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }

    }
  })
}

function getDateStr() {
  //获取当前时间戳
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;
  // console.log("当前时间戳为：" + timestamp);

  //获取当前时间
  var n = timestamp * 1000;
  var date = new Date(n);
  //年
  var Y = date.getFullYear();
  //月
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //日
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  //时
  var h = date.getHours();
  //分
  var m = date.getMinutes();
  //秒
  var s = date.getSeconds();

  // console.log("当前时间：" + Y + M + D + h + ":" + m + ":" + s);

  //转换为时间格式字符串
  console.log(date.toDateString());

  console.log(date.toGMTString());

  console.log(date.toISOString());

  console.log(date.toJSON());

  console.log(date.toLocaleDateString());

  console.log(date.toLocaleString());

  console.log(date.toLocaleTimeString());

  console.log(date.toString());

  console.log(date.toTimeString());

  console.log(date.toUTCString());
}

function getDate() {
  //获取当前时间戳
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;
  // console.log("当前时间戳为：" + timestamp);

  //获取当前时间
  var n = timestamp * 1000;
  var date = new Date(n);
  //年
  var Y = date.getFullYear();
  //月
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //日
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  //时
  var h = date.getHours();
  //分
  var m = date.getMinutes();
  //秒
  var s = date.getSeconds();

  // console.log("当前时间：" + Y + M + D + h + ":" + m + ":" + s);

  return Y + "-" + M + "-" + D;
}

function addYear(index) {
  //获取当前时间戳
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;
  // console.log("当前时间戳为：" + timestamp);

  //获取当前时间
  var n = timestamp * 1000;
  var date = new Date(n);
  //年
  var Y = date.getFullYear();
  //月
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //日
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  //时
  var h = date.getHours();
  //分
  var m = date.getMinutes();
  //秒
  var s = date.getSeconds();

  // console.log("当前时间：" + Y + M + D + h + ":" + m + ":" + s);
  Y += index;

  return Y + "-" + M + "-" + D;
}


function getMinute() {
  var date = new Date();
  //年
  var Y = date.getFullYear();
  //月
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //日
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  //时
  var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  //分
  var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  //秒
  var s = date.getSeconds();

  // console.log("当前时间：" + Y + M + D + h + ":" + m + ":" + s);


  return h + ":" + m;
}


function getFullDate() {
  //获取当前时间戳
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;
  // console.log("当前时间戳为：" + timestamp);

  //获取当前时间
  var n = timestamp * 1000;
  var date = new Date(n);
  //年
  var Y = date.getFullYear();
  //月
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //日
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  //时
  var h = date.getHours();
  //分
  var m = date.getMinutes();
  //秒
  var s = date.getSeconds();

  console.log("当前时间：" + Y + M + D + h + ":" + m + ":" + s);

  return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;
}


function getTime() {
  //获取当前时间戳
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;
  // console.log("当前时间戳为：" + timestamp);

  //获取当前时间
  var n = timestamp * 1000;
  var date = new Date(n);
  //年
  var Y = date.getFullYear();
  //月
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //日
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  //时
  var h = date.getHours();
  //分
  var m = date.getMinutes();
  //秒
  var s = date.getSeconds();

  // console.log("当前时间：" + Y + M + D + h + ":" + m + ":" + s);

  return h + ":" + m;
}

//验证日期（判断结束日期是否大于开始日期）日期格式为YY—MM—DD
function compareDate(startTime, endTime) {
  if (startTime.length > 0 && endTime.length > 0) {
    var startTmp = startTime.split("-");
    var endTmp = endTime.split("-");
    var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
    var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
    if (sd.getTime() > ed.getTime()) {
      alert("开始日期不能大于结束日期");
      return false;
    }
  }
  return true;
}
//验证时间（判断结束时间是否大于结束时间）日期格式为HH—MM—SS
function compareDateTime(startTime, endTime) {
  var now = new Date(endTime).valueOf();
  var time = new Date(startTime).valueOf();
  if (now > time) {
    return true;
  } else {
    return false;
  }
}

function numberFormat(value, index) {
  // console.log(value);
  //var value = parseInt(value)//强转Int，毕竟有可能返回是String类型的数字
  if (index == null) {
    index = 1;
  }
  if (value != null) {
    return value.toFixed(index)
  } else {
    return 0.00;
  }

}

/**
 * 将秒数换成时分秒格式
 * 作者：龙周峰
 */
function formatSeconds(value) {
  var secondTime = parseInt(value); // 秒 
  var minuteTime = 0; // 分 
  var hourTime = 0; // 小时 
  if (secondTime > 60) {
    //如果秒数大于60，将秒数转换成整数 
    //获取分钟，除以60取整数，得到整数分钟 
    minuteTime = parseInt(secondTime / 60);
    //获取秒数，秒数取佘，得到整数秒数 
    secondTime = parseInt(secondTime % 60);
    //如果分钟大于60，将分钟转换成小时 
    if (minuteTime > 60) {
      //获取小时，获取分钟除以60，得到整数小时 
      hourTime = parseInt(minuteTime / 60);
      //获取小时后取佘的分，获取分钟除以60取佘的分 
      minuteTime = parseInt(minuteTime % 60);
    }
  }
  var result = "" + parseInt(secondTime) + "秒";
  if (minuteTime > 0) {
    result = "" + parseInt(minuteTime) + "分" + result;
  }
  if (hourTime > 0) {
    result = "" + parseInt(hourTime) + "小时" + result;
  }
  return result;
}


module.exports = {
  formatTime: formatTime,
  IsChinese: IsChinese,
  checkLocationIsOff: checkLocationIsOff,
  getAgreement: getAgreement,
  getDateStr: getDateStr,
  getDate: getDate,
  getTime: getTime,
  getFullDate: getFullDate,
  compareDateTime: compareDateTime,
  numberFormat: numberFormat,
  formatSeconds: formatSeconds,
  getMinute: getMinute,
  addYear: addYear
}